package org.vectrics.common.view;

/**
 * @author Mike Moore
 * @version 1.0
 */

import org.apache.log4j.Category;
import org.vectrics.common.html.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.ecs.html.Input;
import java.util.*;


abstract public class SimpleFormPageBuilder extends PageBuilder {
    private static Category log = Category.getInstance(SimpleFormPageBuilder.class);
    public static String FONT_PAGE_HEADER = "hdlnMedSltBlu";
    public static String SECTION_DESC_FONT = "lgGray";
    public static String SECTION_FONT = "hdlnDkSltBlu";
    public static String FONT_REGISTER_FIELD_LABEL = "bdDkGray";
    public static String FONT_REGISTER_FIELD_LABEL_ERROR = "bdErrRed";

    public SimpleFormPageBuilder() {
    }

    protected void addReadOnlyText(Table table, String caption, String value) {
        Row row = table.addRow();
        addLabelCell(row, "User", false, false);
        Cell valueCell = row.addCell();
        valueCell.setVAlign("middle");
        valueCell.setClass("ltGray");
        valueCell.addElement(TextHelper.getFont(FONT_REGISTER_FIELD_LABEL, "&nbsp;"
                + value  + "<br><br>"));


    }

    protected Cell addLabelCell(Row row, String caption, boolean required, boolean invalid) {
        Cell labelCell = row.addCell();
        labelCell.setVAlign("top");
        String fontClass = FONT_REGISTER_FIELD_LABEL;

        if (invalid)
            fontClass = FONT_REGISTER_FIELD_LABEL_ERROR;

        String reqStr = "";
        if (required)
            reqStr = "<b> *</b>";

        labelCell.addElement(TextHelper.getFont(fontClass, "&nbsp;"
                + caption + reqStr  + "<br><br>"));
        return(labelCell);
    }

    protected Cell addLabelCell(HttpServletRequest request, Row row, String paramName, String caption, boolean required) {
        boolean invalid = false;
        if (this.getInvalidParam(paramName) != null)
            invalid = true;
        return(addLabelCell(row, caption, required, invalid));
    }

    protected Input makeCheckBox(HttpServletRequest request, String name) {
        Input titleEdit = new Input();
        titleEdit.setType(Input.checkbox);
        titleEdit.setName(name);
        if (this.getParam(request, name).startsWith("t"))
            titleEdit.setChecked(true);
        else
            titleEdit.setChecked(false);
        return(titleEdit);
    }

    protected void addCheckbox(HttpServletRequest request, Table table, String caption, String fldName, boolean required) {
        Cell checkCell = addEntryRow(request, table, fldName, caption, required);
        Input checkbox = makeCheckBox(request, fldName);
        checkCell.addElement(checkbox);
    }


    protected Input makeEdit(HttpServletRequest request, String name) {
        Input titleEdit = new Input();
        titleEdit.setType(Input.text);
        titleEdit.setName(name);
        titleEdit.setValue(getParam(request, name));
        return(titleEdit);
    }

    protected Input makePasswordEdit(HttpServletRequest request, String name) {
        Input titleEdit = makeEdit(request, name);
        titleEdit.setType(Input.password);
        return(titleEdit);
    }


    protected Cell addEntryRow(HttpServletRequest request, Table table, String paramName, String caption, boolean required) {
        Row row = table.addRow();
        addLabelCell(request, row, paramName, caption, required);
        Cell inputCell = row.addCell();
        inputCell.setVAlign("top");

        return(inputCell);
    }


    protected void addSection(Table table, String caption, String desc) {
        Row row = table.addRow();
        Cell cell = row.addCell();
        cell.setColSpan(2);
        cell.addElement(TextHelper.getFont(SECTION_FONT, "<br><br>" + caption + "<br><br>"));
        if (desc != null) {
            cell.addElement(TextHelper.getFont(SECTION_DESC_FONT, desc + "<br><br>"));
        };

    }

    protected void makeEditRow(HttpServletRequest request, Table table, String paramName, String caption, boolean required) {
        Row row = table.addRow();
        addLabelCell(request, row, paramName, caption, required);
        Cell cell = row.addCell();
        cell.setVAlign("top");
        cell.addElement(makeEdit(request, paramName));
    }

    protected void makePasswordEditRow(HttpServletRequest request, Table table, 
    			String paramName, String caption, boolean required) {
        Row row = table.addRow();
        addLabelCell(request, row, paramName, caption, required);
        Cell cell = row.addCell();
        cell.setVAlign("top");
        cell.addElement(makePasswordEdit(request, paramName));
    }

    protected void showInvalidParams(Cell mainCell) {
        if (this.getInvalidParams().size() > 0) {
            log.debug("Invalid properties.  Count = " + this.getInvalidParams().size());
            mainCell.addElement(TextHelper.getFont(FONT_REGISTER_FIELD_LABEL_ERROR,
                    "<br><b>Invalid Entries:</b>"));
            Enumeration invalidEnum = this.getInvalidParams().keys();
            while (invalidEnum.hasMoreElements()) {
                String name = (String)invalidEnum.nextElement();
                String errStr = this.getInvalidParams().getProperty(name);
                mainCell.addElement(TextHelper.getFont(FONT_REGISTER_FIELD_LABEL_ERROR,
                        "<br>&nbsp;&nbsp;*" + errStr));
            }
        }
        else {
            log.debug("No invalid properties");
        }
    }

}